//
//  MainChangeRegionController.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/14.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "MainChangeRegionController.h"

#import "BCHomeDataResponse.h"

#import "MainChangeRegionCell.h"


@interface MainChangeRegionController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *regionTable;

@end

@implementation MainChangeRegionController

- (void)viewDidLoad {
    [super viewDidLoad];
    //
    [self setupSubviews];
}

- (void)setupSubviews {
    [self.customNaviBar.leftButton setImage:nil forState:UIControlStateNormal];
    //
    [self.customNaviBar.leftButton setImage:nil forState:UIControlStateNormal];
    [self.customNaviBar.rightButton setImage:LOAD_IMAGE(@"general_close") forState:UIControlStateNormal];
    self.customNaviBar.rightButton.imageEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 10);
    [self.customNaviBar layoutSubviews];
    //
    self.title = self.homeData.flow.alert_title;
    //
    self.regionTable = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.regionTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.regionTable.dataSource = self;
    self.regionTable.delegate = self;
    [self.view addSubview:self.regionTable];
    [self.regionTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.customNaviBar.mas_bottom);
        make.left.bottom.right.mas_equalTo(self.view);
    }];
    [self.regionTable registerClass:MainChangeRegionCell.class forCellReuseIdentifier:NSStringFromClass(MainChangeRegionCell.class)];
    //
    [self.view layoutIfNeeded];
}

- (void)didClickedNaviRightButton {
    [self dismissViewControllerAnimated:true completion:nil];
}

#pragma mark - UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.homeData.region.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BCHomeDateRegion *region = self.homeData.region[indexPath.row];
    MainChangeRegionCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(MainChangeRegionCell.class)];
    [cell setRegin:region currentRegionId:self.homeData.reg_id];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    BCHomeDateRegion *region = self.homeData.region[indexPath.row];
    if (![self.homeData.reg_id isEqualToString:region.reg_id]) {
        [BCUserManager sharedUser].reg_id = region.reg_id;
        [self dismissViewControllerAnimated:true completion:^{
            [[NSNotificationCenter defaultCenter] postNotificationName:HomeRegionChangedNotification object:nil];
        }];
    }else {
        
    }
}

@end
